package com.echo.im.engin.netty.processor.factory;


import com.echo.im.engin.netty.processor.MessageInProcessor;
import com.echo.im.engin.netty.processor.MessageOutProcessor;
import com.echo.im.engin.netty.processor.MessageProcessor;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  Processor 工厂
 * @author echo
 */
@Component
public class BaseProcessorFactory implements ProcessorFactory {

    private final Map<Integer, MessageInProcessor> inProcessorMap=new HashMap<>();
    private final Map<Integer, MessageOutProcessor> outProcessorMap=new HashMap<>();

    public  BaseProcessorFactory(List<MessageProcessor> processors) {
        for (MessageProcessor processor : processors) {
            if (processor instanceof MessageInProcessor){
                inProcessorMap.put(processor.getCmdType().code(), (MessageInProcessor)processor);
            } else if (processor instanceof MessageOutProcessor) {
                outProcessorMap.put(processor.getCmdType().code(), (MessageOutProcessor)processor);
            }
        }
    }

    @Override
    public  MessageOutProcessor createOutProcessor(Integer cmd) {
        return outProcessorMap.get(cmd);
    }

    @Override
    public  MessageInProcessor createInProcessor(Integer cmd) {
        return inProcessorMap.get(cmd);
    }
}
